package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.File;
import com.frcdi.qms.masterdata.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 附件Controller
 *
 * @author cell
 * @date 2023-09-25
 */
@Api(tags = "附件")
@RestController
@RequestMapping("/masterData/file")
public class FileController extends BaseController
{
    @Autowired
    private IFileService fileService;

    /**
     * 查询附件列表
     */
    @ApiOperation("查询附件列表")
//    @PreAuthorize("@ss.hasPermi('masterData:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(File file)
    {
        startPage();
        List<File> list = fileService.selectFileList(file);
        return getDataTable(list);
    }

    /**
     * 导出附件列表
     */
    @ApiOperation("导出附件列表")
//    @PreAuthorize("@ss.hasPermi('masterData:file:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, File file)
    {
        List<File> list = fileService.selectFileList(file);
        ExcelUtil<File> util = new ExcelUtil<File>(File.class);
        util.exportExcel(response, list, "附件数据");
    }

    /**
     * 获取附件详细信息
     */
    @ApiOperation("获取附件详细信息")
//    @PreAuthorize("@ss.hasPermi('masterData:file:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(fileService.selectFileByFileId(fileId));
    }

    /**
     * 新增附件
     */
    @ApiOperation("新增附件")
//    @PreAuthorize("@ss.hasPermi('masterData:file:add')")
    @PostMapping
    public AjaxResult add(@RequestBody File file)
    {
        return toAjax(fileService.insertFile(file));
    }

    /**
     * 修改附件
     */
    @ApiOperation("修改附件")
//    @PreAuthorize("@ss.hasPermi('masterData:file:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody File file)
    {
        return toAjax(fileService.updateFile(file));
    }

    /**
     * 删除附件
     */
    @ApiOperation("删除附件")
//    @PreAuthorize("@ss.hasPermi('masterData:file:remove')")
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(fileService.deleteFileByFileIds(fileIds));
    }


}
